package input;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.data.extensions.When;
import com.google.gdata.data.extensions.Where;

import calendar.Calendar;
import calendar.Event;
import calendar.Timestamp;

public class GoogleAPIDataProvider extends CalendarDataProvider {
	
	protected static final List<String> eventTaxonomy = Arrays.asList(new String[]{
			"Walking",
			"Running",
			"Swimming",
			"Skating",
			"Yoga",
			"Hiking",
			"Baseball"		
	});
	
	
	private String calendarTitle; 
	private String eventTitle;
	private String eventAspect;
	private String eventLocation;
	private DateTime eventStartTime;
	private DateTime eventEndTime;
	
	private String Year;
	private String Month;
	private String Day;
	private String Hour;
	private String Minute;
	

	@Override
	public Calendar getCalendarData(String publicUrl) {
		
		/*Calendar calendar = new Calendar("1");
		Event event = new Event("Running", 
								new Timestamp("2010","12","13","15","30"),
								new Timestamp("2010","12","13","16","30"));
		calendar.addEvent(event);
		Event event2 = new Event("Climbing", 
				new Timestamp("2010","12","14","15","30"),
				new Timestamp("2010","12","14","16","30"));
		calendar.addEvent(event2);

		*/
		
		// get the calendar from gooogleAPI, for given publicUrl, and
		// then return as a calendar
		
		CalendarService myService;
		Calendar calendar = null;
		calendarTitle ="";
		
    	try{           
            myService = new CalendarService("UNBF-GoogleCalProject-1.0");
            URL feedUrl = new URL(publicUrl);    
            CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
            calendarTitle = resultFeed.getTitle().getPlainText();
            //System.out.println("\t" + calendarTitle);
            
            //calendar = new Calendar(calendarTitle);
            calendar = new Calendar();
            
            CalendarEventFeed myResultsFeed = myService.getFeed(feedUrl, CalendarEventFeed.class);             
            for (int i = 0; i < myResultsFeed.getEntries().size(); i++) {
          	  CalendarEventEntry entry = myResultsFeed.getEntries().get(i);
          	  eventTitle = entry.getTitle().getPlainText();
          	  if (!eventTaxonomy.contains(eventTitle)){
          		  continue;
        	  }
          	  //System.out.println("Event Title: "+entry.getTitle().getPlainText());
          	  List<When> timeList = new ArrayList<When>();
          	  timeList = entry.getTimes();
          	  When time = timeList.get(0);        	  
          	  eventStartTime = time.getStartTime();
          	  parseTime(eventStartTime);
          	  Timestamp startTimeStamp = new Timestamp(Year, Month, Day, Hour, Minute);
          	  eventEndTime = time.getEndTime();
          	  parseTime(eventEndTime);
          	  Timestamp endTimeStamp = new Timestamp(Year, Month, Day, Hour, Minute);
          	  //System.out.println("Event start at: " + time.getStartTime().toString());
          	  //System.out.println("Event end at: " + time.getEndTime().toString());
          	  List<Where> locationList = new ArrayList<Where>();
          	  locationList = entry.getLocations();
          	  Where location = locationList.get(0);
          	  eventLocation = location.getValueString();
          	  //System.out.println("Event location is: "+ location.getValueString());	  
          	  eventAspect = entry.getTextContent().getContent().getPlainText();
          	  //System.out.println("Event description is: " + entry.getTextContent().getContent().getPlainText());
          	  
          	  Event event = new Event(eventTitle,startTimeStamp,endTimeStamp);
          	  
          	  calendar.addEvent(event);
            } 
    	}catch(Exception e){    		
    		System.out.println("Process Google Calendar has problem ...");
    		e.printStackTrace();
    	}
		
		return calendar;
	}
	

	
	private void parseTime(DateTime time){
			
   	  	DateTime selectedTime = time;
   	  	java.util.Calendar date = java.util.Calendar.getInstance();
   	  	date.setTimeInMillis(selectedTime.getValue());
   	  	
		Year = Integer.toString(date.get(java.util.Calendar.YEAR));
		Month = Integer.toString(date.get(java.util.Calendar.MONTH)+1);
		Day = Integer.toString(date.get(java.util.Calendar.DAY_OF_MONTH));
		Hour = Integer.toString(date.get(java.util.Calendar.HOUR_OF_DAY));
		Minute = Integer.toString(date.get(java.util.Calendar.MINUTE));	 	
	}



	public static List<String> getEventTaxonomy() {
		return eventTaxonomy;
	}

	
	
}
